<html>
<head><meta charset="utf-8"><title>built-in macros · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html">built-in macros</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243890835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/243890835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#243890835">(Jun 25 2021 at 09:00)</a>:</h4>
<p>Just looking into prospectively modifying the <code>#[derive(Default)]</code> behavior for if <a href="https://github.com/rust-lang/rfcs/pull/3107">RFC 3107</a> is merged. I've found where the derive is implemented: <code>compiler/rustc_builtin_macros/src/deriving/default.rs</code>, and see that it's currently (and correctly) forbidden on enums. Rust-analyzer tells me the two fields of the <code>StaticEnum</code> variant are <code>EnumDef</code> and <code>Vec&lt;(Ident, Span, StaticFields)&gt;</code>. Where do I start from there?</p>
<p>Assume I know little about how the compiler works, to be honest. The extent of knowledge I have with compilers and ASTs is a single college course where we implemented Scheme and playing around a little bit with Babel. Certainly nothing as complex as Rust's AST and the quite dense code that is this derive macro.</p>



<a name="243891115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/243891115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#243891115">(Jun 25 2021 at 09:03)</a>:</h4>
<p>Importantly, how would I <em>test</em> this while I'm working on it? Diagnostics and all, ideally. How to add new error codes (and what number they should be) wouldn't be bad info to have.</p>



<a name="243895945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/243895945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#243895945">(Jun 25 2021 at 10:00)</a>:</h4>
<blockquote>
<p>Importantly, how would I test this while I'm working on it? Diagnostics and all, ideally.</p>
</blockquote>
<p>With the UI test suite. <code>./x.py test --stage 1 src/test/ui</code>. You'll want to enable incremental compilation in your config.toml, otherwise it takes ages to rebuild on every change. There's a subdir for derives. That doesn't seem to test for any errors though. But you can find examples in the ui test top dir that test error output.</p>



<a name="243898607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/243898607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Wagner-Hall <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#243898607">(Jun 25 2021 at 10:33)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/81642">https://github.com/rust-lang/rust/pull/81642</a> may provide some useful things to crib - it adds a new derive macro for enums, it looks at attributes (Though of the enum itself not the variants), and it has tests that verify that it correctly errors e.g. if an attribute is missing or wrong :)</p>



<a name="243905134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/243905134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#243905134">(Jun 25 2021 at 11:51)</a>:</h4>
<p><span class="user-mention" data-user-id="245610">@Jacob Pratt</span> you might also find the reference docs useful: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_builtin_macros/deriving/generic/enum.SubstructureFields.html#variant.StaticEnum">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_builtin_macros/deriving/generic/enum.SubstructureFields.html#variant.StaticEnum</a></p>



<a name="243980861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/243980861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#243980861">(Jun 25 2021 at 23:14)</a>:</h4>
<p>Thanks everyone! We'll see how helpful that all ends up being <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span></p>



<a name="244005853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/244005853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#244005853">(Jun 26 2021 at 09:44)</a>:</h4>
<p>Making surprisingly good progress for having never touched it before. One question: how do I "register" an attribute? The compiler is currently complaining that the <code>#[default]</code> attribute isn't known. What I'm looking for is the compiler's equivalent of <code>attributes(helper)</code> in <code>#[proc_macro_derive(HelperAttr, attributes(helper))]</code>.</p>



<a name="244005898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/244005898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#244005898">(Jun 26 2021 at 09:44)</a>:</h4>
<p>Or, do I remove the attribute after I'm done with it?</p>



<a name="244014556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/244014556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#244014556">(Jun 26 2021 at 13:14)</a>:</h4>
<p><span class="user-mention" data-user-id="245610">@Jacob Pratt</span> I don't know, but <code>register_attr</code> led me to <a href="https://github.com/rust-lang/rust/issues/66080">https://github.com/rust-lang/rust/issues/66080</a> so maybe try that?</p>



<a name="244049347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/244049347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#244049347">(Jun 27 2021 at 04:36)</a>:</h4>
<p>Unfortunately that doesn't seem to lead me in the right direction. It appears to be only for global attributes, which isn't what's desired here.</p>



<a name="244050502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/built-in%20macros/near/244050502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/built-in.20macros.html#244050502">(Jun 27 2021 at 05:17)</a>:</h4>
<p>Just reading a bit with regard to whether the attribute should be removed. It shouldn't: helper attributes are inert (<a href="https://doc.rust-lang.org/reference/procedural-macros.html#derive-macro-helper-attributes">source</a>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>